
package me.tuyou.xml;


import org.springframework.core.io.ClassPathResource;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.io.InputStream;


public class StaxTest {

    public static void main(String[] args) throws IOException, XMLStreamException {

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        ClassPathResource resource = new ClassPathResource("activiti.cfg.xml");
        InputStream inputStream = resource.getInputStream();
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            if (event == XMLStreamConstants.START_DOCUMENT) {
                System.out.println("文档开始");
            } else if (event == XMLStreamConstants.END_DOCUMENT) {
                System.out.println("文档结束");
            } else if (event == XMLStreamConstants.START_ELEMENT) {
                System.out.println("-------开始元素---------");
                System.out.println(xmlStreamReader.getLocalName());
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                System.out.println("-------结束元素---------");
            } else if (event == XMLStreamConstants.CHARACTERS) {
                String text = xmlStreamReader.getText();
                System.out.println("读取文本元素:" + text);
            }
        }
    }
}
